package br.edu.fasete.javaroad.plugin.amateras;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import br.edu.fasete.javaroad.model.architeture.Variable;
import br.edu.fasete.javaroad.plugin.ProjectConfigurations;

public class WizardPageVariaveis extends WizardPage {
	private Composite composite;
	private Composite frame;
	private Map<String, Text> inputs;

	private ProjectConfigurations configurations;

	public WizardPageVariaveis(ProjectConfigurations configurations) {
		super("Output Folder");
		setTitle("Configure Architeture Parameters");
		this.configurations = configurations;
	}

	public void setVisible(boolean v) {
		if(v) {
			montarControle();
		}
		else {
			atualizarVariaveis();
		}
		super.setVisible(v);
	}
	
	public void atualizarVariaveis() {
		for(String k: inputs.keySet()) {
			configurations.getArchiteture().getVariables().setValue(k, inputs.get(k).getText());
		}
	}
	
	public void createControl(Composite parent) {
		getShell().setText("generate.dialog.title");
		composite = new Composite(parent, SWT.NULL);
		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		setControl(composite);
	}
	
	public void montarControle() {
		if(frame != null) {
			frame.setVisible(false);
			frame.dispose();
		}

		frame = new Composite(composite, SWT.NULL);
		frame.setLayout(new GridLayout(2, false));
		frame.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		inputs = new HashMap<String, Text>();
		for(Variable v: configurations.getArchiteture().getVariableList()) {
			Label label = new Label(frame, SWT.NULL);
			label.setText(v.getName());
			
			Text t = new Text(frame, SWT.BORDER);
			t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			t.setText(v.getValue());
			t.setToolTipText(v.getDescription());
			
			inputs.put(v.getName(), t);
		}
		
		composite.layout(true);
		composite.getParent().getParent().layout(true);
	}
}
